package com.huatai.exchange.utils;

import com.faneui.common.exception.BusinessException;
import com.faneui.common.utils.ListUtils;
import com.faneui.dal.DBUtils;
import com.faneui.service.Dbconfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.util.List;
import java.util.Map;

@Component
public class TaFundUtils {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    // 查询净值是否存在
    public String checkFundNavIsFinish(String fundCode, String occurDate) throws BusinessException {
        Connection conn = null;
        String user = "ta4_query";
        String r;
        try {
            conn = DBUtils.getConnection(Dbconfig.TA4DB, user, Dbconfig.getPasswordByUserId(user));
            String sql = "select * from htta4.tfundday t where c_fundcode = ? and d_date = ? ";
            List<Map<String, Object>> result = DBUtils.executeQuery(conn, sql, fundCode, occurDate);
            r = ListUtils.isEmpty(result) ? "[" + occurDate + "][" + fundCode + "]净值未出" : "1";
        } catch (Exception ex) {
            logger.error("", ex);
            r = ex.getMessage();
        } finally {
            DBUtils.closeAll(conn);
        }
        return r;
    }

    // 查询净值是否存在
    public List<Map<String, Object>> getFundNavList(String fundCode, String startDate, String endDate) throws BusinessException {
        Connection conn = null;
        String user = "ta4_query";
        List<Map<String, Object>> result = null;
        try {
            conn = DBUtils.getConnection(Dbconfig.TA4DB, user, Dbconfig.getPasswordByUserId(user));
            String sql = "select * from htta4.tfundday t where c_fundcode = ?  and d_date > ? and d_date <= ? " +
                    " order by d_date desc ";
            result=  DBUtils.executeQuery(conn, sql, fundCode, startDate,endDate);
        } catch (Exception ex) {
            logger.error("", ex);
        } finally {
            DBUtils.closeAll(conn);
        }
        return result;
    }

}
